#pragma once

#include  "../comm/log.hpp"
#include  "../comm/util.hpp"

#include <string>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <fstream>
#include <cassert>
#include <unordered_map>
#include <jsoncpp/json/json.h>

// 根据题目list文件, 加载所有题目到内存中
// model: 主要用来和数据进行交互, 对外提供访问数据的接口
using namespace std;
using namespace ns_log;
using namespace ns_util;

namespace ns_model
{
    struct Question//题目属性
    {
        std::string number; // 题目编号
        std::string title; // 题目名称
        std::string difficulty ; // 题目难度
        int cpu_limit; // 题目时间要求(s)
        int memory_limit; // 题目空间要求(KB)
        std::string desc; // 题目描述
        std::string header; //给用户的预设代码
        std::string tail; // 测试用例, 需要跟header拼接, 形成完整代码
    };

    const std::string question_list = "./questions/questions.list";
    const std::string question_path = "./questions/";

    class Model
    {
    private:
        //题号: 题目细节
        unordered_map<string, Question> questions;
    public:
        Model()
        {
            assert(LoadQuestionList(question_list));
        }
        bool LoadQuestionList(const string &question_list)
        {
            // 加载配置文件: questions/questions.list + 题目编号文件
            ifstream in(question_list);
            if(!in.is_open()) //配置文件打开失败
            {
                LOG(FATAL) << "加载题库失败, 请检查是否存在题库文件" << "\n";
                return false;
            }

            string line;
            while(getline(in, line))
            {
                vector<string> tokens;
                StringUtil::SplitString(line, &tokens, " ");
                if(tokens.size()!= 5)
                {
                    LOG(WARNNING) << "加载部分题目, 请检查文件格式" << "\n";
                    continue;
                }
                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.difficulty = tokens[2];
                q.cpu_limit = atoi(tokens[3].c_str());
                q.memory_limit = atoi(tokens[4].c_str());

                string quesnum_path = question_path;
                quesnum_path += q.number;
                quesnum_path += "/";

                FileUtil::ReadFile(quesnum_path+"desc.txt", &(q.desc), true);
                FileUtil::ReadFile(quesnum_path+"header.cpp", &(q.header), true);
                FileUtil::ReadFile(quesnum_path+"tail.cpp", &(q.tail), true);

                questions.insert({q.number, q});
            }
            LOG(INFO) << "加载题库成功"<< "\n";
            in.close();
            return true;
        }

        bool GetAllQuestions(vector<Question> *out)
        {
            if(questions.size() == 0)
            {
                LOG(ERROR) << "用户获取题库失败" << "\n";
                return false;
            }
            for(const auto &q : questions)
            {
                out->push_back(q.second);
            }
            return true;
        }
        bool GetOneQuestion(const std::string &number, Question *q)
        {
            const auto& it = questions.find(number);
            if(it == questions.end())
            {
                LOG(ERROR) << "用户获取题目失败, 题目编号: "<< number << "\n";
                return false;
            }
            (*q) = it->second;
            return true;
        }
        ~Model()
        {}
    };
} // namespace ns_model
